﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using apitest.Module;
namespace apitest.Controllers
{
    [ApiController]
    [Route("order")]
    public class WeatherForecastController : ControllerBase
    {
        public OrderService service = new OrderService();
        //private static readonly string[] Summaries = new[]
        //{
        //    "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
        //};

        //private readonly ILogger<WeatherForecastController> _logger;

        //public WeatherForecastController(ILogger<WeatherForecastController> logger)
        //{
        //    _logger = logger;
        //}

        //[HttpGet]
        //public IEnumerable<WeatherForecast> Get()
        //{
        //    var rng = new Random();
        //    return Enumerable.Range(1, 5).Select(index => new WeatherForecast
        //    {
        //        Date = DateTime.Now.AddDays(index),
        //        TemperatureC = rng.Next(-20, 55),
        //        Summary = Summaries[rng.Next(Summaries.Length)]
        //    })
        //    .ToArray();
        //}

        [HttpGet]
        public Order Get()
        {
            string id = "1", date = "2", name = "3", phoneNumber = "4";
            int age = 5;
            int kind = 1;
            double amount = 2, money = 2;
            List<OrderDetail> details = new List<OrderDetail>();
            OrderDetail detail = new OrderDetail(amount, money, kind);
            details.Add(detail);
            service.addOrder(id, date, name, phoneNumber, age, details);
            return service.searchOrder(1, id);
        }

        [HttpGet("{id}")]
        public Order Get(int iid)
        {
            string id = "1", date = "2", name = "3", phoneNumber = "4";
            int age = 5;
            int kind = 1;
            double amount = 3, money = 3;
            List<OrderDetail> details = new List<OrderDetail>();
            OrderDetail detail = new OrderDetail(amount, money, kind);
            details.Add(detail);
            service.addOrder(id, date, name, phoneNumber, age, details);
            return service.searchOrder(1, id);
        }

        [HttpPost]
        public Sorder Post([FromBody] Opera value)
        {
            Order order = null;
            Sorder sorder = new Sorder();
            sorder.good = value.good;
            sorder.id = value.id;
            sorder.price = value.price;
            sorder.totalMoney = value.totalMoney;
            sorder.amount = "1";
            switch (value.op) {
                case "1": add(value); break;
                case "2": delete(value); break;
                case "3": order= search(value); break;
                case "4": change(value); break;
            }
            if (order!=null&& value.op=="3")
            {
                sorder.amount = order.details[0].amount.ToString();
            }
            Console.WriteLine("post");
            Console.WriteLine(value.id);
            return sorder;
        }
        private void add(Opera opera)
        {
            int kind = int.Parse(opera.good);
            List<OrderDetail> details = new List<OrderDetail>();
            OrderDetail detail = new OrderDetail(1, double.Parse(opera.price), kind);
            details.Add(detail);
            service.addOrder(opera.id, "1", "1", "1", 1, details);
        }


        private void delete(Opera opera)
        {
            try
            {
                service.removeOrder(opera.id);
            }
            catch
            {

            }
        }

        private Order search(Opera opera)
        {
            return service.searchOrder(1, opera.id);
        }
        private void change(Opera opera)
        {
            int kind = int.Parse(opera.good);
            List<OrderDetail> details = new List<OrderDetail>();
            OrderDetail detail = new OrderDetail(1, double.Parse(opera.price), kind);
            details.Add(detail);
            service.changeOrder(2, opera.id, null, 0, null, details);
        }
    }
    
}
